package liang.eight.edition.model.view.controller;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MVCDemo extends JApplet {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		MVCDemo applet = new MVCDemo();
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("MVCDemo");
		frame.getContentPane().add(applet, BorderLayout.CENTER);
		frame.setSize(400, 120);
		frame.setVisible(true);
	}

	public MVCDemo() {

		JButton jbtController = new JButton("Show Controller");
		JButton jbtView = new JButton("Show View");

		final Model model = new Model();

		setLayout(new FlowLayout());
		add(jbtController);
		add(jbtView);

		jbtController.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame frame = new JFrame("Controller");
				Controller controller = new Controller();

				controller.setModel(model);

				frame.add(controller);
				frame.setSize(500, 500);
				frame.setLocation(500, 500);
				frame.setVisible(true);
			}
		});

		jbtView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFrame frame = new JFrame("View");
				View view = new View();

				view.setModel(model);

				frame.add(view);
				frame.setSize(500, 500);
				frame.setLocation(500, 500);
				frame.setVisible(true);
			}
		});
	}
}
